﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace CSharpToObjectiveC
{
    class Program
    {
        static void Main(string[] args)
        {
            String assemblyPath = string.Empty;
            if (args.Length > 0)
            {
                assemblyPath = args[0];
            }
            else
            {
                Console.Write("Path to .dll to convert ");
                assemblyPath = Console.ReadLine();
            }
            
            if (!System.IO.File.Exists(assemblyPath))
            {
                Console.WriteLine("File does not exist");
                Console.ReadLine();
            }
            else
            {
                Assembly assem = Assembly.LoadFrom(assemblyPath);

                Type[] types = assem.GetTypes();

                for (Int32 i = 0; i < types.Length - 1; i++)
                {
                    Console.WriteLine(String.Format("{0}.{1} ({1})", types[i].Namespace, types[i].Name));
                }

                Console.Write("What type to convert? ");
                String typeName = Console.ReadLine();

                Type selectedType = types.First(t => t.Name == typeName);

                MethodInfo[] methods = selectedType.GetMethods();

                for (Int32 j = 0; j < methods.Length - 1; j++)
                {
                    //Console.WriteLine(String.Format("[{0}] ({1}) {2} -> {3}", j, methods[j].IsPublic ? "Public" : "Private", methods[j].Name, methods[j].ReturnType));
                    Console.WriteLine(String.Format("[{0}] {1} {2} -> {3}", j, methods[j].IsStatic ? "+" : "-", methods[j].Name, methods[j].ReturnType));
                }

                String h = string.Empty;
                String m = string.Empty;
                StringBuilder sb = new StringBuilder();

                //Build h file
                sb.AppendLine("//Converted by dugongsoftware at codeplex.com ");
                sb.AppendLine("#import <Foundation/Foundation.h>");
                sb.AppendLine();
                sb.AppendFormat("@interface {0} : NSObject", typeName);
                sb.AppendLine();
                sb.AppendLine("{");

                //Do properties
                for (Int32 j = 0; j < methods.Length - 1; j++)
                {
                    if (methods[j].Name.StartsWith("get_"))
                    {
                        String objectiveCType = Engine.TypeConvertor.ToObjectiveC(methods[j].ReturnType);
                        sb.AppendFormat("   {0} {1};", objectiveCType, methods[j].Name.Substring(4));
                        sb.AppendLine();
                    }
                }

                sb.AppendLine("}");

                sb.AppendLine();

                //Do properties
                for (Int32 j = 0; j < methods.Length - 1; j++)
                {
                    if (methods[j].Name.StartsWith("get_"))
                    {
                        String objectiveCType = Engine.TypeConvertor.ToObjectiveC(methods[j].ReturnType);
                        sb.AppendFormat("@property (nonatomic, strong) {0} {1};", objectiveCType, methods[j].Name.Substring(4));
                        sb.AppendLine();
                    }
                }

                sb.AppendLine();
                sb.AppendLine("@end");

                h = sb.ToString();

                Console.WriteLine(h);
                Console.ReadLine();
            }
        }
    }
}
